package net.zjitc.controller;

import net.zjitc.pojo.Category;
import net.zjitc.pojo.CategorySecond;
import net.zjitc.service.CategorySecondService;
import net.zjitc.service.CategoryService;
import net.zjitc.service.impl.CategorySecondSerImpl;
import net.zjitc.service.impl.CategoryServiceImpl;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by tt on 2017/11/18.
 */
public class AdminCategorySecond {
    CategoryService service=new CategoryServiceImpl();
    CategorySecondService secondService=new CategorySecondSerImpl();
    //二级菜单显示
    @URLMapping(url = "/adminCategorySecond")
    public String adminCategorySecondList(HttpServletRequest request, HttpServletResponse response){
        List<CategorySecond> list=secondService.findAll();
        request.setAttribute("lists",list);
        return "/admin/second/secondList.jsp";
    }
    //添加页面显示
    @URLMapping(url = "/addUICategorySecond")
    public String addUICategorySecond(HttpServletRequest request, HttpServletResponse response){
        List<Category> list=service.findAll();
        request.setAttribute("clist",list);
        return "/admin/second/add.jsp";
    }
    //添加
    @URLMapping(url = "/addCategorySecond")
    public String addCategorySecond(HttpServletRequest request, HttpServletResponse response){
        Category category=new Category();
        CategorySecond categorySecond=new CategorySecond();
        try {
            BeanUtils.populate(category,request.getParameterMap());
            BeanUtils.populate(categorySecond,request.getParameterMap());
            categorySecond.setCategory(category);
            int count=secondService.addCategorySecond(categorySecond);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return "Redirect:"+request.getContextPath()+"/adminCategorySecond";
    }
    //回显
    @URLMapping(url = "/editUICategorySecond")
    public String editUICategorySecond(HttpServletRequest request, HttpServletResponse response){
        String csid=request.getParameter("csid");
        CategorySecond categorySecond=secondService.findByCsid(csid);
        request.setAttribute("cs",categorySecond);
        List<Category> list=service.findAll();
        request.setAttribute("clist",list);
        return "/admin/second/edit.jsp";
    }
    //编辑
    @URLMapping(url = "/editCategorySecond")
    public String editCategorySecond(HttpServletRequest request, HttpServletResponse response){
        CategorySecond categorySecond=new CategorySecond();
        Category category=new Category();
        try {
            BeanUtils.populate(category,request.getParameterMap());
            BeanUtils.populate(categorySecond,request.getParameterMap());
            categorySecond.setCategory(category);
            int count=secondService.editCategorySecond(categorySecond);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return "Redirect:"+request.getContextPath()+"/adminCategorySecond";
    }
    //删除
    @URLMapping(url = "/deleteCategorySecond")
    public String deleteCategorySecond(HttpServletRequest request, HttpServletResponse response){
        String csid=request.getParameter("csid");
        int count=secondService.deleteCategorySecond(csid);
        return "Redirect:"+request.getContextPath()+"/adminCategorySecond";
    }

}
